// 编译指令建议：javac -encoding UTF-8 ModEvents.java
package com.example.radiationarea.event;

import com.example.radiationarea.RadiationAreaMod;
import com.example.radiationarea.block.ModBlocks;
import com.example.radiationarea.effect.ModEffects;
import com.example.radiationarea.effect.RadiationEffect;
import com.example.radiationarea.util.RadiationSuitHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

// Import TerraBlender classes if needed
// import some.terrablender.classes.Here;

@Mod.EventBusSubscriber(modid = RadiationAreaMod.MOD_ID)
public class ModEvents {
    private static final Logger LOGGER = LogManager.getLogger();
    
    // This event handler can be used to add additional radiation effect logic
    // For example, if a player has radiation effect, additional effects can be added here
    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
        Player player = event.player;
        
        // Only process on server side and on the first phase for each player
        if (event.phase == TickEvent.Phase.START && !player.level.isClientSide()) {
            // 检查玩家是否穿戴全套防辐射装备
            if (RadiationSuitHelper.isPlayerWearingFullRadiationSuit(player)) {
                // 移除辐射效果
                if (player.hasEffect(ModEffects.RADIATION.get())) {
                    player.removeEffect(ModEffects.RADIATION.get());
                    LOGGER.debug("Removed radiation effect from player {} due to full radiation suit", player.getName().getString());
                }
                // 不施加新的辐射效果
                return;
            }
            
            // 检查玩家是否站在放射性方块上
            BlockPos playerPos = player.blockPosition();
            boolean onRadioactiveBlock = player.level.getBlockState(playerPos.below()).is(ModBlocks.CONTAMINATED_DIRT.get());
            
            if (onRadioactiveBlock) {
                // 检查玩家是否穿戴任意防辐射装备
                if (RadiationSuitHelper.isPlayerWearingAnyRadiationSuit(player)) {
                    // 降低辐射效果等级
                    if (player.hasEffect(ModEffects.RADIATION.get())) {
                        MobEffectInstance effect = player.getEffect(ModEffects.RADIATION.get());
                        int amplifier = effect.getAmplifier();
                        
                        // 如果当前效果等级大于0，则降低一级
                        if (amplifier > 0) {
                            player.removeEffect(ModEffects.RADIATION.get());
                            player.addEffect(new MobEffectInstance(ModEffects.RADIATION.get(), 600, amplifier - 1));
                            LOGGER.debug("Reduced radiation effect level for player {}: level={}", player.getName().getString(), amplifier - 1);
                        }
                    } else {
                        // 给予1级辐射效果而不是默认等级
                        player.addEffect(new MobEffectInstance(ModEffects.RADIATION.get(), 600, 0));
                        LOGGER.debug("Applied reduced radiation effect to player {}", player.getName().getString());
                    }
                } else {
                    // 没有穿戴防辐射装备，正常施加辐射效果
                    // Only apply effect if not already present or about to expire
                    MobEffectInstance effect = player.getEffect(ModEffects.RADIATION.get());
                    if (effect == null || effect.getDuration() <= 10) {
                        player.addEffect(new MobEffectInstance(ModEffects.RADIATION.get(), 600, 0));
                        LOGGER.debug("Applied radiation effect to player {}", player.getName().getString());
                    }
                }
            }
            // 如果不在放射性方块上，则不主动移除效果，让其自然过期
            
            // 处理辐射效果造成的伤害
            if (player.hasEffect(ModEffects.RADIATION.get())) {
                MobEffectInstance effect = player.getEffect(ModEffects.RADIATION.get());
                int amplifier = effect.getAmplifier();
                
                // Apply damage based on effect level
                // Level 0 = 1 heart per second
                // Level 1 = 2 hearts per second
                // Level 2 = 3 hearts per second, and so on
                float damage = (amplifier + 1) * 0.5f; // Damage per second value
                
                // Apply damage every 20 ticks (1 second)
                if (player.tickCount % 20 == 0) {
                    player.hurt(RadiationEffect.RADIATION_DAMAGE, damage);
                    LOGGER.debug("Applied radiation damage to player {}: damage={}", player.getName().getString(), damage);
                }
            }
        }
    }
}